package com.sanyue.collections;

import org.junit.Test;

import java.util.PriorityQueue;

/**
 * @author sanyue
 * @version 1.0
 * @description: TODO
 * @date 2022/3/13 下午12:46
 * 1、add()与offer()：都是在队列尾部增加元素，区别在于队列满时，add方法会抛出队列已满异常，offer方法返回false表示队列已满。
 * 2、remove()与poll()：都是删除队列的头元素，remove方法在队列为空的情况下将抛异常，而poll方法将返回null。
 * 3、element()与peek()：都是返回队列的头元素，但不删除头元素，element方法在队列为空的情况下，将抛异常，而peek方法将返回null。
 *
 */
public class PriorityQueueStudy {
class Student{
    public Student() {

    }
    public Student(int age, int height) {
        this.age = age;
        this.height = height;
    }

    int age;
    int height;

    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", height=" + height +
                '}';
    }
}
    @Test
    public void test(){
        PriorityQueue<Student> queue = new PriorityQueue<>((a, b) -> {
            if (a.height == b.height)
                return a.age - b.age;
            else
                return a.height - b.height;
        });
        Student s1 = new Student(10, 170);
        Student s2 = new Student(11, 170);
        Student s3 = new Student(12, 160);
        Student s4 = new Student(12, 180);
        queue.offer(s4);
        queue.offer(s1);
        queue.offer(s3);
        queue.offer(s2);

        while (!queue.isEmpty()){
            System.out.println(queue.poll().toString());
        }

    }
}
